package org.aiforum.backend.controller;

import java.util.List;

import org.aiforum.backend.common.QueryPreperty;
import org.aiforum.backend.common.Response;
import org.aiforum.backend.service.AuthService;
import org.aiforum.domain.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 权限管理控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/auth")
public class AuthController {
	
   @Autowired
   private AuthService authService;
   
   @RequestMapping(value={"/all"})
   @ResponseBody
   public Response all(){
	   QueryPreperty queryPreperty=new QueryPreperty();
	   queryPreperty.setQueryName("state");
	   queryPreperty.setQueryValue(1);
	   //插入权限
	   List<Auth> auths=authService.querys(queryPreperty);
	   Response response=Response.getInstaner();
	   response.setState(auths.size()>0);
	   response.setData(auths);
	   return response;
   }
   
   @RequestMapping(value={"/insert"},method={RequestMethod.POST})
   @ResponseBody
   public ModelAndView insert(Auth auth){
	   
	   //插入权限
	   Response response=authService.insert(auth);
	   //创建视图模型对象
	   ModelAndView modelAndView=new ModelAndView();
	   //重定向到消息页
	   modelAndView.setViewName("redirect:../message");
	   //设置消息数据
	   modelAndView.addObject("message", response.getMessage());
	   return modelAndView; 
   }
   
   /**
    * 根据属性查询权限
    */
   @RequestMapping(value={"/querys"})
   public ModelAndView query(QueryPreperty queryPreperty,PageInfo page){
	   //设置分页参数
	   //PageHelper.starPage(当前页码，每页显示的记录数量，是否查询总页数)
	   PageHelper.startPage(page.getPageNum(), page.getPageSize(), true);
	   
	   List<Auth> auths=authService.querys(queryPreperty);
	   page=new PageInfo<Auth>(auths);
	   ModelAndView modelAndView=new ModelAndView();
	   modelAndView.setViewName("auth/list");
	   modelAndView.addObject("auths", auths);
	   modelAndView.addObject("queryPreperty", queryPreperty);
	   modelAndView.addObject("page", page);
	   return modelAndView;
   }
       /**
        * 根据id查找
        * @return
        */
       @RequestMapping(value={"/find/{authId}"},method={RequestMethod.GET})
       public ModelAndView find(@PathVariable Integer authId){
    	   Auth auth=authService.find(authId);
    	   ModelAndView modelAndView=new ModelAndView();
    	   modelAndView.setViewName("auth/update");
    	   modelAndView.addObject("auth",auth);
    	   return modelAndView;
       }
       /**
        * 更新权限
        * @param auth
        * @return
        */
       @RequestMapping(value={"/modify"},method={RequestMethod.POST})
       public ModelAndView modify(Auth auth){
    	   //插入权限
    	   Response response=authService.update(auth);
    	   ModelAndView modelAndView=new ModelAndView();
    	   modelAndView.setViewName("redirect:../message");
    	   modelAndView.addObject("message", response.getMessage());
    	   return modelAndView;
       }
	   /**
	    * 删除权限
	    */
	   @RequestMapping(value={"/delete/{authId}"},method={RequestMethod.GET})
	   public ModelAndView delet(@PathVariable Integer authId){
		   Response response=authService.delete(authId);
		   ModelAndView modelAndView=new ModelAndView();
		   modelAndView.setViewName("redirect:../../message");
		   modelAndView.addObject("message",response.getMessage());
		   return modelAndView;
	   }
	   
	   @RequestMapping(value={"/checkExists"},method={RequestMethod.GET})
	   @ResponseBody
	   public Response exists(QueryPreperty queryPreperty){
		   Response response=authService.checkExists(queryPreperty);
		   return response;
	   }
}
